package it.francescogabbrielli.android.solidalapp.actions;

import it.francescogabbrielli.android.solidalapp.Action;
import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.file.RequestFileActivity;
import it.francescogabbrielli.android.solidalapp.requestendpoint.Requestendpoint;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Message;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Response;

/**
 * Send a chat message from request side
 */
public class GCMInsertRequestMessage extends Action {
	
	@Override
	public void execute() throws Exception {
		Response res = getModel(Response.class);
		Message msg = new Message();
		msg.setId(res.getId());
		msg.setDeviceId(res.getDeviceId());
		msg.setMessage(res.getMessage());
		setModel(msg);
		Requestendpoint endpoint = EndpointsUtils.buildEndpoint(Requestendpoint.class);
		Message ret = endpoint.insertMessage(msg).execute();
		setModel(ret);
	}
	
	@Override
	public void onError(String error) {
		refireActivity(RequestFileActivity.class);
	}

	@Override
	public void onPostExecute() throws Exception {
		refireActivity(RequestFileActivity.class);
	}

}
